package com.bbkb.quartz.util;


import com.bbkb.common.constants.CommonConstant;
import com.bbkb.user.dto.SysMenuBtnDTO;
import com.bbkb.user.dto.SysMenuDTO;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TreeUtil {

    private final static String MENU_ID = "menu_";

    private final static String BTN_ID = "btn_";

    List<SysMenuDTO> menus;

    List<SysMenuDTO> rootMenus;
    List<SysMenuDTO> childMenus;
    List<SysMenuBtnDTO> childBtns;

    public TreeUtil(List<SysMenuDTO> menus) {
        this.menus = menus;
    }

    public TreeUtil(List<SysMenuDTO> menus, List<SysMenuBtnDTO> childBtns) {
        this.childBtns = childBtns;
        this.menus = menus;
    }

    public TreeUtil(List<SysMenuDTO> rootMenus, List<SysMenuDTO> childMenus,
                    List<SysMenuBtnDTO> childBtns) {
        this.rootMenus = rootMenus;
        this.childMenus = childMenus;
        this.childBtns = childBtns;
    }

    private TreeNode menuToNode(SysMenuDTO menu) {
        if (menu == null) {
            return null;
        }
        TreeNode node = new TreeNode();
        node.setId(menu.getId());
        node.setText(menu.getMenuName());
        node.getAttributes().put("type", "0");
        node.getAttributes().put("id", menu.getId());
        node.getAttributes().put("url", menu.getMenuUrl());
//		if("0".equals(menu.getEnd())){
//			node.setState("closed");
//		}
        return node;
    }

    private TreeNode BtnToNode(SysMenuBtnDTO btn) {
        if (btn == null) {
            return null;
        }
        TreeNode node = new TreeNode();
        node.setId(btn.getId());
        node.setText(btn.getBtnName());
        node.getAttributes().put("type", "1");
        node.getAttributes().put("id", btn.getId());
        return node;
    }

    public List<TreeNode> getRootNodes() {
        List<TreeNode> rootNodes = new ArrayList<>();
        for (SysMenuDTO menu : menus) {
            if (0L == menu.getParentId()) {
                TreeNode node = menuToNode(menu);
                if (node != null) {
                    addChlidNodes(node);
                    rootNodes.add(node);
                }
            }
        }
        return rootNodes;
    }

    private void addChlidNodes(TreeNode rootNode) {
        List<TreeNode> childNodes = new ArrayList<>();
        for (SysMenuDTO menu : menus) {
            if (rootNode.getId().equals(menu.getParentId())) {
                TreeNode node = menuToNode(menu);
                if (childBtns != null && !childBtns.isEmpty()) {
                    addChlidBtn(node);
                }
                if (CommonConstant.YES != menu.getEnd()) {
                    addChlidNodes(node);
                }
                childNodes.add(node);
            }
        }
        rootNode.setChildren(childNodes);
    }

    private void addChlidBtn(TreeNode treeNode) {
        List<TreeNode> childNodes = new ArrayList<TreeNode>();
        for (SysMenuBtnDTO btn : childBtns) {
            if (treeNode.getId().equals(btn.getMenuId())) {
                TreeNode node = BtnToNode(btn);
                childNodes.add(node);
            }
        }
        treeNode.setChildren(childNodes);
    }

    @Data
    public static class TreeNode {

        private Long id;

        private String text;

        private String state;

        private Boolean checked;

        private String iconCls;

        private List<TreeNode> children;

        private Map<String, Object> attributes = new HashMap<>();

    }
}
